package icmp

import (
	"fmt"
	"golang.org/x/net/context"
	"nprobe/core"
	"nprobe/core/agent"
	"nprobe/core/aggregate"
)

// ICMPAgent icmp echo
type ICMPAgent struct {
	agent.BaseAgent
}

func NewICMPAgent(name string, task *core.ProbeTask) (*ICMPAgent, error) {
	salts := core.NewSalts(task.MsgLen)

	ctx, cancelFunc := context.WithCancel(context.Background())
	receiverGroup := NewICMPReceiverGroup(ctx, name, task, 10000, salts)
	senderGroup, err := NewICMPSenderGroup(ctx, name, task, 10000, salts, len(receiverGroup.Receivers))
	if err != nil {
		fmt.Printf("nprobe error: %v\n", err)
		return nil, err
	}

	aggregator := aggregate.NewRingBufAggregator(ctx, name, task, senderGroup, receiverGroup)

	return &ICMPAgent{
		BaseAgent: agent.BaseAgent{
			Name:          name,
			Task:          task,
			SenderGroup:   senderGroup,
			ReceiverGroup: receiverGroup,
			Aggregator:    aggregator,

			Ctx:        ctx,
			CancelFunc: cancelFunc,
		},
	}, nil
}
